﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MinhTam.DataAccess.Web;
using MinhTam.Objects;
using System.Data;
public partial class QuanLyVanPhong : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            HelpControls.InitListTinhThanh(ddlTinhThanh);
            HelpControls.InitListLoaiVanPhong(ddlLoaiVanPhong);
            InitListData();
            txtMoTa.config.toolbar = HelpUtil.GetSimpleCKEditorConfig(this);
            txtMoTa.config.uiColor = HelpUtil.GetCKEditorUiColorAdmin(this);

        }
    }

    private void InitListData()
    {
        string sql = @"SELECT ROW_NUMBER() OVER (ORDER BY vp.LOAI_VP_ID,tt.ten_tinh_thanh, vp.ten_van_phong) STT,vp.*,lvp.TEN_LOAI_VP, tt.TEN_TINH_THANH,'abc' GROUPNAME
        FROM VAN_PHONG vp
        LEFT JOIN LOAI_VAN_PHONG lvp ON lvp.LOAI_VP_ID = vp.LOAI_VP_ID
        LEFT JOIN TINH_THANH tt ON tt.TINH_THANH_ID=vp.TINH_THANH_ID
        ";
        DataTable dt = SQLConnectWeb.GetTable(sql, VAN_PHONG.sTableName);
        gridViewData.DataKeyNames = new string[] {
            VAN_PHONG.cl_VAN_PHONG_ID, 
            VAN_PHONG.cl_TEN_VAN_PHONG,
            VAN_PHONG.cl_DIA_CHI, 
            VAN_PHONG.cl_DIEN_THOAI,VAN_PHONG.cl_FAX,VAN_PHONG.cl_MAP_URL,
            VAN_PHONG.cl_LOAI_VP_ID,
            VAN_PHONG.cl_TINH_THANH_ID,
            VAN_PHONG.cl_MO_TA,
        VAN_PHONG.cl_EMAIl,
        VAN_PHONG.cl_IS_LIEN_HE,
        VAN_PHONG.cl_HOT_LINE};
        gridViewData.AutoGenerateColumns = false;
        gridViewData.DataSource = dt;
        gridViewData.DataBind();

    }
    protected void gridViewData_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        DataKey row = gridViewData.DataKeys[int.Parse(e.CommandArgument.ToString())];
        if (e.CommandName == "cmdEdit")
        {
            //   GridViewRow row = gridViewData.Rows[];

            keyField.Value = row.Values[VAN_PHONG.cl_VAN_PHONG_ID].ToString();
            txtTenVanPhong.Text = row.Values[VAN_PHONG.cl_TEN_VAN_PHONG].ToString();
            txtDiaChi.Text = row.Values[VAN_PHONG.cl_DIA_CHI].ToString();
            txtURL.Text = row.Values[VAN_PHONG.cl_MAP_URL].ToString();
            txtSoDienThoai.Text = row.Values[VAN_PHONG.cl_DIEN_THOAI].ToString();
            txtSoFax.Text = row.Values[VAN_PHONG.cl_FAX].ToString();
            txtMoTa.Text = row.Values[VAN_PHONG.cl_MO_TA].ToString();
            ddlTinhThanh.SelectedValue = row.Values[VAN_PHONG.cl_TINH_THANH_ID].ToString();
            ddlLoaiVanPhong.SelectedValue = row.Values[VAN_PHONG.cl_LOAI_VP_ID].ToString();
            txtHotline.Text = row.Values[VAN_PHONG.cl_HOT_LINE].ToString();
            txtEmail.Text = row.Values[VAN_PHONG.cl_EMAIl].ToString();
            ckcLienHe.Checked = row.Values[VAN_PHONG.cl_IS_LIEN_HE].ToString() == "Y";

            btnSave.Text = "Cập nhật";

        }
        else if (e.CommandName == "cmdDelete")
        {
            
            string delete = row.Value.ToString();
            // HelpControls.ShowMsgBoxConfirmDelete(this);
            string sql = string.Format("delete from {0} where {1}={2}", VAN_PHONG.sTableName, VAN_PHONG.cl_VAN_PHONG_ID, delete);
            string islienhe = row.Values[VAN_PHONG.cl_IS_LIEN_HE].ToString();
            if (SQLConnectWeb.ExecSQL(sql))
            {
                //  HelpControls.ShowMsgBox(this, "Xóa dữ liệu thành công!");
                if (islienhe == "Y")
                {
                    string sqlU = @"UPDATE VAN_PHONG
                            SET
                            IS_LIEN_HE = 'Y'
                            WHERE VAN_PHONG_ID =  
                            (SELECT TOP 1 vp.VAN_PHONG_ID
                            FROM VAN_PHONG vp
                            ORDER BY vp.LOAI_VP_ID
                            )";
                    SQLConnectWeb.ExecSQL(sqlU);
                }
                InitListData();
                if (delete == keyField.Value)
                    btnClearNew_Click(null, null);
            }
            else
            {
                HelpControls.ShowMsgBox(this, "Dữ liệu đang được sử dụng!");
            }

        }
    }

    protected void btnClearNew_Click(object sender, EventArgs e)
    {
        txtDiaChi.Text = "";
        txtSoDienThoai.Text = "";
        txtSoFax.Text = "";
        txtTenVanPhong.Text = "";
        keyField.Value = "";
        txtMoTa.Text = "";
        txtURL.Text = "";
        txtEmail.Text = "";
        txtHotline.Text = "";
        ckcLienHe.Checked = false;
        btnSave.Text = "Lưu";

    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(keyField.Value))//insert
        {
            bool ok = true;
            if (ckcLienHe.Checked)
            {
                string sqlUpdate = string.Format(@"UPDATE VAN_PHONG SET IS_LIEN_HE = 'N'");
                ok = SQLConnectWeb.ExecSQL(sqlUpdate);
            }
            VAN_PHONG obj = null;
            if (ok)
            {

                obj = VAN_PHONG.Insert_Object(txtTenVanPhong.Text, txtDiaChi.Text, txtSoDienThoai.Text, txtSoFax.Text, ddlTinhThanh.SelectedValue, txtMoTa.Text.Replace("'", ""), txtURL.Text.Replace("'", ""), ddlLoaiVanPhong.SelectedValue, txtHotline.Text, txtEmail.Text, ckcLienHe.Checked ? "Y" : "N");
                if (obj == null)
                    ok = false;
            }
            if (ok)
            {
                InitListData();
                keyField.Value = obj.VAN_PHONG_ID;
                btnSave.Text = "Cập nhật";
            }
            else HelpControls.ShowMsgBox(this, "Lưu dữ liệu thất bại!");

        }
        else
        {
            bool ok = true;
            if (ckcLienHe.Checked)
            {
                string sqlUpdate = string.Format(@"UPDATE VAN_PHONG SET IS_LIEN_HE = 'N'");
                ok = SQLConnectWeb.ExecSQL(sqlUpdate);
            } if (ok)
            {
                VAN_PHONG vp = new VAN_PHONG();
                vp.VAN_PHONG_ID = keyField.Value;
                ok = vp.Save_Object(txtTenVanPhong.Text, txtDiaChi.Text, txtSoDienThoai.Text, txtSoFax.Text, ddlTinhThanh.SelectedValue, txtMoTa.Text.Replace("'", ""), txtURL.Text.Replace("'", ""), ddlLoaiVanPhong.SelectedValue, txtHotline.Text, txtEmail.Text, ckcLienHe.Checked ? "Y" : "N");

            }
            if (ok)
            {
                InitListData();
            }
            else HelpControls.ShowMsgBox(this, "Lưu dữ liệu thất bại!");
        }
    }

    protected void gridViewData_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ((LinkButton)e.Row.Cells[1].Controls[0]).OnClientClick = "return confirm('Bạn có chắc là muốn xóa dữ liệu này?');";
            DataRowView v = e.Row.DataItem as DataRowView;
            RadioButton c = ((RadioButton)e.Row.Cells[2].FindControl("rdLienHe"));
            c.Checked = v["IS_LIEN_HE"].ToString() == "Y";

        }
    }

    protected void rdLienHe_CheckedChanged(object sender, EventArgs e)
    {
        RadioButton c = (RadioButton)sender;
        System.Web.UI.WebControls.GridViewRow row = (System.Web.UI.WebControls.GridViewRow)(c.Parent.Parent);
        string id=gridViewData.DataKeys[row.DataItemIndex].Value.ToString();

        string sql = string.Format(@"UPDATE VAN_PHONG
        SET
        IS_LIEN_HE = CASE WHEN VAN_PHONG_ID = {0} THEN 'Y' ELSE 'N' END",id );
        if (SQLConnectWeb.ExecSQL(sql))
        {
            InitListData();
        }
        if (id == keyField.Value)
            ckcLienHe.Checked = true;
    }
}
